<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta charset="UTF-8">
<title>添加资讯</title>

<link rel="stylesheet" href="layui/css/layui.css">
<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
<script src="layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 --> 

</head>
<body>
<center><br><h2>添加资讯</h2><br><br></center>

<form class="layui-form" action=""  enctype="multipart/form-data">
  <!-- <div class="layui-form-item">
    <label class="layui-form-label">长输入框</label>
    <div class="layui-input-block">
      <input type="text" name="title" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">短输入框</label>
    <div class="layui-input-inline">
      <input type="text" name="username" required lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
    </div>
  </div> -->
  <div class="layui-form-item">
<!--     <label class="layui-form-label">商品编号</label>
    <div class="layui-input-inline">
      <input type="text" name="pid" required value="" lay-verify="required" placeholder="请输入商品编号 [确保唯一]" autocomplete="off" class="layui-input">
    </div> -->
    <label class="layui-form-label">添加标题</label>
    <div class="layui-input-inline">
      <input type="text" name="title" required value="" lay-verify="required" placeholder="请输入商品名称" autocomplete="off" class="layui-input">
    </div>
  </div>
  <!-- <div class="layui-form-item">
  	<label class="layui-form-label">市场价</label>
    <div class="layui-input-inline">
      <input type="text" name="marketprice" required value="" lay-verify="required|number" placeholder="请输入市场价" autocomplete="off" class="layui-input">
    </div>
    <label class="layui-form-label">优惠价</label>
    <div class="layui-input-inline">
      <input type="text" name="price" required value="" lay-verify="required|number" placeholder="请输入优惠价" autocomplete="off" class="layui-input">
    </div>
  </div> -->
<!--   <div class="layui-form-item">
  	<label class="layui-form-label">库存量</label>
    <div class="layui-input-inline">
      <input type="text" name="number" required value="" lay-verify="required|number" placeholder="请输入商品库存" autocomplete="off" class="layui-input">
    </div>
    <label class="layui-form-label">单 位</label>
    <div class="layui-input-inline">
      <input type="text" name="unit" required value="" lay-verify="required" placeholder="请输入商品单位" autocomplete="off" class="layui-input">
    </div>
  </div> -->
  <!-- <div class="layui-form-item">
    <label class="layui-form-label">密码框</label>
    <div class="layui-input-inline">
      <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
    </div>
    <div class="layui-form-mid layui-word-aux">辅助文字</div>
  </div> -->
  <div class="layui-form-item">
    <label class="layui-form-label">上传类型</label>
    <div class="layui-input-inline">
      <select name="type" lay-verify="required">
        <option value="视频">视频</option>
        <option value="图片">图片</option>
        <option value="文本">文本</option>
      </select>
    </div>
  </div>
  <!-- <div class="layui-form-item">
    <label class="layui-form-label">复选框</label>
    <div class="layui-input-block">
      <input type="checkbox" name="like[write]" title="写作">
      <input type="checkbox" name="like[read]" title="阅读" checked>
      <input type="checkbox" name="like[dai]" title="发呆">
    </div>
  </div> -->

<!--   <div class="layui-form-item">
	    <div class="layui-inline">
	              <label class="layui-form-label">日期选择</label>
			      <div class="layui-input-block">
			      <input type="text" name="starttime" id="date1" autocomplete="off" class="layui-input">
			      </div>
	    </div>
	    
	    <div class="layui-inline">
	          <label class="layui-form-label">日期选择</label>
		      <div class="layui-input-block">
		      <input type="text" name="endtime" id="date2" autocomplete="off" class="layui-input">
		      </div>
	    </div>  
</div> -->
<!--   <div class="layui-form-item">
    <label class="layui-form-label">上架</label>
    <div class="layui-input-block">
      <input type="checkbox" name="switch" lay-skin="switch">
      设置value="1"可自定义值，否则选中时返回的就是默认的on lay-text="是|否"
      <input type="checkbox" name="state" lay-skin="switch" checked value="1" lay-text="是|否">
    </div>
  </div>
  <div class="layui-form-item layui-form-text">
    <label class="layui-form-label">商品规格</label>
    <div class="layui-input-block">
      <textarea name="specification" required value="" lay-verify="required" placeholder="请输入内容" class="layui-textarea"></textarea>
    </div>
  </div> -->
   <div class="layui-form-item layui-form-text">
    <label class="layui-form-label">上传文件</label>
    <div class="layui-input-block">
       <input type="file" name="file1" class="file1" id="test20">
    </div>
  </div>

  <div class="layui-form-item layui-form-text">
    <label class="layui-form-label">公告内容</label>
    <div class="layui-input-block">
      <textarea name="content" value="" placeholder="请输入内容" class="layui-textarea"></textarea>
    </div>
  </div>
  
 <div class="layui-form-item">
    <label class="layui-form-label">状态</label>
    <div class="layui-input-block">
      <input type="radio" name="state" value="1" title="发布" checked>
      <input type="radio" name="state" value="0" title="未发布" >
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" id="submitbut" lay-submit lay-filter="formDemo">添加</button>
      <button type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>


<script>
$(function(){
	$("input[name='id']").blur(function(){
		var id=$(this).val();
		$.post("<%=basePath%>getproductbypid.do",{ id:id },function(res){
			if(res.code==1){
				$("input[name='id']").focus();//锁定文本框状态
				layer.msg('当前商品编号已被使用，请输入新的编号！', {icon:5, time:2000});
			}
		});
		
	});
	
	
});

//创建实例  要想数据初始化 这里必须要写
layui.use(['table', 'layer', 'form', 'laydate'], function () {
	var table = layui.table;
	layer = layui.layer;
	form = layui.form;
	laydate = layui.laydate;
	 //日期
	  laydate.render({
	    elem: '#date1'
	  });

	  laydate.render({
	    elem: '#date2'
	  });
  //监听提交
  form.on('submit(formDemo)', function(data){
	var param=data.field;//定义临时变量获取表单提交过来的数据，非json格式
	var data=JSON.stringify(param);//表格数据序列化
	//console.log(data);//测试是否获取到表单数据，调试模式下在页面控制台查看
	//layer.msg(data);
    
    $.ajax({
		url:"<%=basePath%>addinfo.do",//接口
        type:'post',//method请求方式，get或者post
        dataType:'json',//预期服务器返回的数据类型
        data:JSON.stringify(param),//表格数据序列化
        contentType: "application/json; charset=utf-8",
        success: function(res){//res为相应体,function为回调函数
        	if (res.code == 1) {
				layer.msg(res.msg, { icon: 1, time: 1500 });
	            //layer.alert(res.msg, {icon:1});
	            $("#reset").click();//调用重置按钮将表单数据清空
	            //layer.close(index);  //关闭弹窗
			} else{
				layer.msg(res.msg, { icon: 2, time: 1500 });
				//layer.alert(res.msg, {icon: 5});
			}
        },
        error: function(){
        	layer.msg("操作失败", { icon: 2 });
        	//layer.alert('操作失败', {icon:5});
        }
	}); 
	return false;//阻止表单跳转。如果需要表单跳转,去掉这段即可，但会影响弹框效果。
	
   <%--  $.post("<%=basePath%>addproduct.do", { product: data }, function (ret){
        if (ret.code == "1") {
            layer.msg(ret.msg, { icon: 1, time: 1500 }, function () {
                $(".layui-laypage-btn").click();
            });
            //layer.close(index);  //关闭弹窗
        } else {
            layer.msg(ret.msg, { icon: 2, time: 1500 });
        }
    });
    //return false; --%>
    
  });
  
});
</script>

</body>
</html>